package com.monkey.controller;

import com.monkey.core.ErrorCode;
import com.monkey.core.WebRet;
import com.monkey.model.comparator.MonkeyTradeSort;
import com.monkey.model.monkey.MonkeyTradeData;
import com.monkey.model.user.User;
import com.monkey.model.user.UserData;
import com.monkey.service.MarketService;
import com.monkey.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * desc:
 * auth: chenxiaobin
 * data: 2018/1/4.
 */
@RestController
@RequestMapping(value = "/market")
public class MarketController {
    private static Logger logger = LoggerFactory.getLogger(MarketController.class);

    @Resource
    private UserService userService = null;

    @Resource
    private MarketService marketService = null;

    /**
     * 挂单到市场
     *
     * @param id    int 宠物id
     * @param price string 价格
     */
    @RequestMapping(value = "/sell", method = RequestMethod.POST)
    public Map<String, Object> sell_log(@RequestBody Map<String, Object> params, @RequestHeader String token) {
        Map<String, Object> ret = new HashMap<>();
        if (null == token || token.isEmpty()) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        User user = userService.getUserWithToken(token);
        if (null == user) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        int monkeyId = (int) params.get("id");
        String price = (String) params.get("price");
        if (0 == monkeyId || null == price || price.isEmpty()) {
            ret.put("code", ErrorCode.PARAM_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.PARAM_ERROR));
            return ret;
        }

        WebRet ret1 = marketService.sell(user, monkeyId, Float.valueOf(price));
        ret.put("code", ret1.getCode());
        ret.put("msg", ret1.getMsg());
        return ret;
    }

    /**
     * 撤销市场挂单
     *
     * @param id int 宠物id
     */
    @RequestMapping(value = "/cancel", method = RequestMethod.POST)
    public Map<String, Object> cancel_log(@RequestBody Map<String, Object> params, @RequestHeader String token) {
        Map<String, Object> ret = new HashMap<>();
        if (null == token || token.isEmpty()) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        UserData user = userService.getInfo(token);
        if (null == user) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        int monkeyId = (int) params.get("id");
        if (0 == monkeyId) {
            ret.put("code", ErrorCode.PARAM_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.PARAM_ERROR));
            return ret;
        }

        WebRet ret1 = marketService.cancel(user.getId(), monkeyId);
        ret.put("code", ret1.getCode());
        ret.put("msg", ret1.getMsg());
        return ret;
    }

    /**
     * 获取市场列表
     *
     * @param p      bool 价格(true从小到大，false从大到小，未选中就不传)
     * @param g      bool 代数(true从小到大，false从大到小，未选中就不传)
     * @param w      bool 体重(true从小到大，false从大到小，未选中就不传)
     * @param r      bool 掘金(true从小到大，false从大到小，未选中就不传)
     * @param id     int 可选，非空非0表示选择指定宠物
     * @param s      int 指定代数，-1表示不指定
     * @param offset int 偏移量
     * @param num    int 数量
     * @return list 宠物列表
     */
    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    public Map<String, Object> getList(@RequestBody Map<String, Object> params, @RequestHeader String token) {
        Map<String, Object> ret = new HashMap<>();
        int price = 0;
        int generation = 0;
        int weight = 0;
        int reproduct = 0;
        if (params.containsKey("p")) {
            price = ((boolean) params.get("p")) ? 1 : -1;
        }
        if (params.containsKey("g")) {
            generation = ((boolean) params.get("g")) ? 1 : -1;
        }
        if (params.containsKey("w")) {
            weight = ((boolean) params.get("w")) ? 1 : -1;
        }
        if (params.containsKey("r")) {
            reproduct = ((boolean) params.get("r")) ? 1 : -1;
        }
        int id = (int) params.getOrDefault("id", 0);
        int specify = (int) params.get("s");
        int offset = (int) params.get("offset");
        int num = (int) params.get("num");
        if (0 > offset || 0 > num) {
            ret.put("code", ErrorCode.PARAM_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.PARAM_ERROR));
            return ret;
        }
        offset = (offset - 1) * num;
        List<MonkeyTradeData> monkeys = marketService.getList(id,
                new MonkeyTradeSort(price, generation, weight, reproduct, specify, offset, num));
        if (null == monkeys || monkeys.isEmpty()) {
            ret.put("code", ErrorCode.GET_DATA_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.GET_DATA_ERROR));
        }
        ret.put("code", ErrorCode.SUCCESS);
        ret.put("msg", "");
        ret.put("list", monkeys);
        return ret;
    }

    /**
     * 市场支付
     *
     * @param id int 市场上的宠物id
     */
    @RequestMapping(value = "/pay", method = RequestMethod.POST)
    public Map<String, Object> pay_log(@RequestBody Map<String, Object> params, @RequestHeader String token) {
        Map<String, Object> ret = new HashMap<>();
        if (null == token || token.isEmpty()) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        User user = userService.getUserWithToken(token);
        if (null == user) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }

        String price = (String) params.get("price");
        int monkeyId = (int) params.get("id");
        if (null == price || price.isEmpty() || 0 == monkeyId) {
            ret.put("code", ErrorCode.PARAM_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.PARAM_ERROR));
            return ret;
        }

        WebRet ret1 = marketService.market(user, monkeyId, Float.valueOf(price));
        ret.put("code", ret1.getCode());
        ret.put("msg", ret1.getMsg());
        return ret;
    }
}
